﻿namespace EasyGo.Data
{
    using System.Data.Entity;
    using EasyGo.Data.QueryServices;
    using EasyGo.Data.Repositories;
    using StructureMap;

    public class DataConfigurator
    {
        public static void Configure()
        {
            ObjectFactory.Configure(
                container =>
                {
                    container.For<DbContext>().HttpContextScoped().Use<ApplicationDbContext>();
                    ConfigureRepositories(container);
                    ConfigureQueryServices(container);
                });
        }

        private static void ConfigureRepositories(ConfigurationExpression container)
        {
            container.Scan(
                scan =>
                    {
                        scan.AssemblyContainingType<DataConfigurator>();
                        scan.IncludeNamespaceContainingType<UserRepository>();
                        scan.AddAllTypesOf(typeof(RepositoryBase<,>));
                        scan.Convention<RepositoriesRegistration>();
                    });
        }

        private static void ConfigureQueryServices(ConfigurationExpression container)
        {
            container.Scan(
                scan =>
                {
                    scan.AssemblyContainingType<DataConfigurator>();
                    scan.IncludeNamespaceContainingType<UserQueries>();
                    scan.RegisterConcreteTypesAgainstTheFirstInterface();
                });
        }
    }
}
